<template>
	<div>
		<vxe-modal
			v-model="show" id="add" width="60%" height="80vh" :size="size" :position="{top: '10%'}"
			@close="closeForm()" @show="open" show-zoom resize transfer show-footer destroy-on-close>
			<!-- storage 将窗口拖动的状态保存到本地 remember 再次打开窗口时还原窗口状态-->
			<!--标题-->
			<template #title>
				<span style="font-weight: bold;">修改</span>
			</template>
			<!--主体-->
			<template #default>
				<el-form :size="size" ref="form" :model="form" class="form" :rules="rules" label-width="100px">
					<!-- 用户名 -->
					<el-row >
						<el-col :span="24">
							<el-form-item  prop="username">
								<template slot="label">
									<span>用户名 </span>
									<el-tooltip content="用户登录名称" placement="top-start">
										<i class="el-icon-warning-outline"></i>
									</el-tooltip>
								</template>
								<el-input tabindex="1"  v-model="form.username" autoComplete="off" clearable  placeholder="请输入用户名">
								</el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<!-- 真实姓名 -->
					<el-row >
						<el-col :span="24">
							<el-form-item  prop="realname">
								<template slot="label">
									<span>真实姓名 </span>
								</template>
								<el-input tabindex="3"  v-model="form.realname" autoComplete="off" clearable  placeholder="请输入真实姓名">
								</el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<!-- 头像 -->
					<el-row >
						<el-col :span="24">
							<el-form-item  prop="headimg">
								<template slot="label">
									<span>头像 </span>
								</template>
								<ImagesUpload :size="size"  uploadType="2" fileType="image" :image.sync="form.headimg"></ImagesUpload>
							</el-form-item>
						</el-col>
					</el-row>
					<!-- 邮箱 -->
					<el-row >
						<el-col :span="24">
							<el-form-item  prop="email">
								<template slot="label">
									<span>邮箱 </span>
								</template>
								<el-input tabindex="7"  v-model="form.email" autoComplete="off" clearable  placeholder="请输入邮箱">
								</el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<!-- 手机号 -->
					<el-row >
						<el-col :span="24">
							<el-form-item  prop="mobile">
								<template slot="label">
									<span>手机号 </span>
								</template>
								<el-input tabindex="9"  v-model="form.mobile" autoComplete="off" clearable  placeholder="请输入手机号">
								</el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<!-- 所属角色 -->
					<el-row >
						<el-col :span="24">
							<el-form-item  prop="role_id">
								<template slot="label">
									<span>所属角色 </span>
								</template>
								<el-checkbox-group v-model="form.role_id">
									<el-checkbox v-for="(item,i) in role_ids" :key="i" :label="item.value.toString()">{{item.label}}</el-checkbox>
								</el-checkbox-group>
							</el-form-item>
						</el-col>
					</el-row>
					<!-- 所属部门 -->
					<el-row >
						<el-col :span="24">
							<el-form-item  prop="dept_id">
								<template slot="label">
									<span>所属部门 </span>
								</template>
								<Treeselect :class="size" :default-expand-level="1" v-model="form.dept_id" :options="dept_ids" :normalizer="normalizer" :show-count="true" zIndex="999999" placeholder="请选择所属部门"/>
							</el-form-item>
						</el-col>
					</el-row>
					<!-- 数据权限 -->
					<el-row >
						<el-col :span="24">
							<el-form-item  prop="permission">
								<template slot="label">
									<span>数据权限 </span>
								</template>
								<el-select tabindex="15"  style="width:100%" v-model="form.permission" filterable clearable placeholder="请选择">
									<el-option key="0" label="全部数据权限" :value="1"></el-option>
									<el-option key="1" label="本部门及以下数据权限" :value="2"></el-option>
									<el-option key="2" label="本部门数据权限" :value="3"></el-option>
									<el-option key="3" label="本人数据权限" :value="4"></el-option>
									<el-option key="4" label="指定部门的数据权限" :value="5"></el-option>
									<el-option key="5" label="无数据权限" :value="6"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>
					<!-- 自定义部门 -->
					<el-row v-if="form.permission == '5'">
						<el-col :span="24">
							<el-form-item  prop="dept_ids">
								<template slot="label">
									<span>自定义部门 </span>
								</template>
								<el-select tabindex="17"  multiple style="width:100%" v-model="form.dept_ids" filterable clearable placeholder="请选择">
									<el-option v-for="(item,i) in dept_idss" :key="i" :label="item.label" :value="item.value.toString()"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>
					<!-- 过期时间 -->
					<el-row >
						<el-col :span="24">
							<el-form-item  prop="expiration">
								<template slot="label">
									<span>过期时间 </span>
								</template>
								<el-date-picker value-format="yyyy-MM-dd HH:mm:ss" type="datetime" v-model="form.expiration" clearable placeholder="请输入过期时间"></el-date-picker>
							</el-form-item>
						</el-col>
					</el-row>
					<!-- 备注 -->
					<el-row >
						<el-col :span="24">
							<el-form-item  prop="note">
								<template slot="label">
									<span>备注 </span>
								</template>
								<el-input tabindex="21"  v-model="form.note" autoComplete="off" clearable  placeholder="请输入备注">
								</el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<!-- 排序 -->
					<el-row >
						<el-col :span="24">
							<el-form-item  prop="sort_id">
								<template slot="label">
									<span>排序 </span>
								</template>
								<el-input-number controls-position="right" style="width:200px;" autoComplete="off" v-model="form.sort_id" clearable :min="0" placeholder="排序"/>
							</el-form-item>
						</el-col>
					</el-row>
					<!-- 状态 -->
					<el-row >
						<el-col :span="24">
							<el-form-item  prop="status">
								<template slot="label">
									<span>状态 </span>
								</template>
								<el-switch :active-value="1" :inactive-value="0" v-model="form.status"></el-switch>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</template>
			<!--底部-->
			<template #footer>
				<el-button :size="size" :loading="loading" type="primary" @click="submit">
					<span v-if="!loading">确 定</span><span v-else>提 交 中...</span>
				</el-button>
				<el-button :size="size" @click="closeForm">取 消</el-button>
			</template>
		</vxe-modal>
	</div>
</template>
<script>
import { update ,getFieldList } from '@/api/admin/admin/user'
import ImagesUpload from '@/components/common/ImagesUpload.vue'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import mixinVexTable from "@/mixin/vxeTable"
export default {
	name:'admin_userupdate',
	mixins: [mixinVexTable],
	components: {
		ImagesUpload,
		Treeselect,
	},
	props: {
		show: {
			type: Boolean,
			default: false
		},
		size: {
			type: String,
			default: 'small'
		},
		info: {
			type: Object,
		},
	},
	data(){
		return {
			form: {
				username:'',
				realname:'',
				headimg:'',
				email:'',
				mobile:'',
				role_id:['1'],
				dept_id:1,
				permission:1,
				dept_ids:[],
				expiration:null,
				note:'',
				status:1,
				update_time:'',
			},
			role_ids:[],
			dept_ids:[],
			dept_idss:[],
			loading:false,
			rules: {
				username:[
					{required: true, message: '用户名不能为空', trigger: 'blur'},
				],
				email:[
					{pattern:/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/, message: '邮箱格式错误'}
				],
				mobile:[
					{pattern:/^1[3456789]\d{9}$/, message: '手机号格式错误'}
				],
			},
		}
	},
	watch:{
		show(value){
			if(value){
				getFieldList().then(res => {
					if(res.status == 200){
						this.role_ids = res.data.role_ids
						this.dept_ids = res.data.dept_ids
						this.dept_idss = res.data.dept_idss
					}
				})
			}
		},
	},
	methods: {
		open(){
			this.form = this.info
			this.setDefaultVal('role_id')
			if(this.info.dept_id == '0' ){
				this.$delete(this.info,'dept_id')
			}
			this.setDefaultVal('dept_ids')
		},
		submit(){
			this.$refs['form'].validate(valid => {
				if(valid) {
					this.loading = true
					if(!this.form.role_id) this.form.role_id = 0;
					if(!this.form.dept_id) this.form.dept_id = 0;
					if(!this.form.dept_ids) this.form.dept_ids = 0;
					update(this.form).then(res => {
						if(res.status == 200){
							this.$message({message: '操作成功', type: 'success'})
							this.$emit('refesh_list')
							this.closeForm()
						}
					}).catch(()=>{
						this.loading = false
					})
				}
			})
		},
		setDefaultVal(key){
			if(this.form[key] == null || this.form[key] == ''){
				this.form[key] = []
			}
		},
		normalizer(node) {
			if (node.children && !node.children.length) {
				delete node.children
			}
			return {
				id: node.value,
				label: node.label,
				children: node.children
			}
		},
		closeForm(){
			this.$emit('update:show', false)
			this.loading = false
			if (this.$refs['form']!==undefined) {
				this.$refs['form'].resetFields()
			}
		},
	}
}
</script>
<style scoped lang="scss">
@import '@/assets/scss/common.scss'
</style>
